import { getAreaObj, DateDiffer } from '@/utils/util'
import { getStore } from '@/utils/store'

const userInfo = getStore('userInfo') || {}

export const formConfig = {
	inline: false,
	showBtn: true,
	labelPosition: 'top',
	labelWidth: '120px',
	column: [
		{
			label: '器材名称',
			prop: 'name',
			type: 'input',
			maxlength: '100',
			showWordLimit: true,
			span: 12,
		},
		{
			label: '放置位置',
			prop: 'placePoint',
			propName: 'placePointName',
			type: 'BuildRoomBed',
			span: 12,
			deepLevel: 1,
			formDispaly: true,
			display: !(userInfo.classifyId == 4 && userInfo.userType == 3),
		},
		{
			label: '具体位置',
			prop: 'specificPoint',
			type: 'input',
			span: 12,
		},
		{
			label: '放置数量',
			prop: 'placeNum',
			type: 'input',
			span: 12,
		},
		{
			label: '过期时间',
			prop: 'overdueTime',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 12,
			rules: [{ required: true, message: '请选择过期时间' }],
		},
	],
}

export const searchConfig = {
	inline: true,
	showBtn: true,
	labelPosition: 'left',
	column: [
		{
			label: '所属地区',
			prop: 'areas',
			type: 'AplidAreaSelect',
			span: 8,
		},
		{
			label: '所属单位',
			prop: 'organizationName',
			type: 'input',
			span: 8,
		},
		{
			label: '放置位置',
			prop: 'placePoint',
			type: 'input',
			display: !(userInfo.classifyId == 4 && userInfo.userType == 3),
			span: 8,
		},
		{
			label: '放置数量',
			prop: 'placeNum',
			type: 'input',
			span: 8,
		},
		{
			label: '过期时间',
			prop: 'overdueTime',
			type: 'date',
			format: 'YYYY年MM月DD日',
			valueFormat: 'YYYY-MM-DD',
			span: 8,
		},
	],
}

export const tableConfig = {
	stripe: true,
	border: true,
	columns: [
		{ label: '所属区', prop: 'areaName' },
		{ label: '所属单位', prop: 'organizationName' },
		{ label: '器材名称', prop: 'name' },
		{
			label: '放置位置',
			prop: 'placePointName',
			hide: userInfo.classifyId == 4 && userInfo.userType == 3,
		},
		{ label: '放置数量', prop: 'placeNum' },
		{ label: '过期时间', prop: 'overdueTime' },
		{ label: '距离过期剩余天数', prop: 'diffDay' },
		{
			label: '操作',
			prop: 'menu',
			width: 300,
			fixed: 'right',
		},
	],
}

export const api = {
	index: 'view_pbRespirator_page',
	add: 'view_pbRespirator_add',
	edit: 'view_pbRespirator_edit',
	delete: 'view_pbRespirator_del',
	export: 'view_pbRespirator_export',
	authArea: 'authArea',
}
export const baseConfig = {
	pageName: '呼吸器管理',
	dictcodes: [],
	rowKey: 'id',
	addLabel: '新增',
	tipLabel: 'name',
	exportMqTopic: '/ning/export/',
	loadOrgInfo: true,
	api,
	auth: {
		...api,
		mulitSet: 'mulitSet',
	},
	paramsObj: {
		source: 1,
	},
	menuBtn: [
		{
			icon: 'editor',
			label: '编辑',
			type: 'primary',
			operate: 'edit',
			auth: api.edit,
			text: true,
			show: true,
		},
		{
			icon: 'detail',
			label: '详情',
			type: 'primary',
			operate: 'detail',
			auth: api.index,
			text: true,
			show: true,
		},
		{
			icon: 'delete',
			label: '删除',
			type: 'danger',
			operate: 'delete',
			auth: api.delete,
			text: true,
			show: true,
		},
	],
	dialogConfig: {
		width: '55%',
	},
	searchConfig,
	formConfig,
	tableConfig,
	detailConfig: formConfig,
	onLoadFormate: (arr) => {
		return new Promise((resolve, reject) => {
			const temp = arr.map((item) => {
				const day = DateDiffer(item.overdueTime)
				const str = day > 0 ? `距离过期剩余${day}天` : `已过期 ${Math.abs(day)}天`

				if (item.placePoint) {
					item.placePoint = item.placePoint.replace('层', '').split('-')
					item.placePointName = item.placePoint.join('-') + '层'
				}

				return {
					...item,
					diffDay: day == 0 ? '今天过期' : str,
				}
			})
			resolve(temp)
		})
	},
	saveFormate: (form) => {
		return new Promise((resolve, reject) => {
			if (form.placePoint) {
				form.placePoint = form.placePoint.join('-') + '层'
			}
			resolve({
				...form,
			})
		})
	},
	beforeOnLoadFormate: async (params) => {
		return new Promise((resolve, reject) => {
			const areaObj: any = getAreaObj(params.areas)
			resolve({
				...params,
				...areaObj,
				areas: undefined,
			})
		})
	},
}
